# -*- coding: utf-8 -*-

__author__ = "Sérgio Severino, 9733"
__version__ = "v1.0 from 2/12/2011"

'''
Source: http://matplotlib.sourceforge.net/mpl_examples/pylab_examples/barchart_demo.py
slightly changed
'''

import numpy as np
import matplotlib.pyplot as plt
from database import Database, DB



class DrawBarChart:
    '''
    A class that draws bar charts
    using matplotlib
    '''

    @staticmethod
    def createBarChartOfNTeachersPerEstablishmentByYear( data, establishment ):
        '''
        This method draws a bar char with the number of teachers in a chosen
        establishment along eleven years(2000-2010)
        receives a Database data
        receives an establishment
        '''

        #number of bars = number of years
        nBars = len( data.getDifYears() )
        nTeachers = []

        # gets the quantity of teachers per year
        for year in data.getDifYears():
            nTeachers.append(data.getNTheachersPerEstablishmentPerYear( establishment, year ))
            pass
        
        teachersPerYear = tuple(nTeachers)
        
        ind = np.arange(nBars)  # the x locations for the bars
        width = 0.5       # the width of the bars
        
        
        plt.subplot(111)
        rects1 = plt.bar(ind + width * 3 / 2, teachersPerYear, width, color='b')
        
        
        plt.ylabel('Number of Teachers')
        plt.xlabel('Year')
        plt.title('Number of Teachers In\n' + establishment.decode('utf-8'))

        # x bar scale
        plt.xticks( ind + width * 2, (tuple(data.getDifYears())) )

        # y max scale value
        ymax = max(nTeachers)
        ymax = ymax * 13 / 10
        if ymax <= 10:
            ymax = 12
            pass
        
        # y scale
        yscale = max(nTeachers) / 5
        if yscale == 0:
            yscale = 1
            pass
        
        plt.yticks(xrange(0, ymax, yscale))
        
        def autolabel(rects):
            # attach some text labels on top of each bar
            for rect in rects:
                height = rect.get_height()
                plt.text(rect.get_x() + rect.get_width()/2., 1.001*height, '%d'%int(height),
                        ha='center', va='bottom')
        
        autolabel(rects1)

        plt.show()
        pass

    @staticmethod
    def createBarChartOfNTeachersPerCategoriePerEstablishmentPerYear( data, categorie, establishment ):
        '''
        This method draws a bar char with the number of teachers in a chosen
        establishment and categorie along eleven years(2000-2010)
        receives a Database data
        receives an establishment
        '''

        #number of bars = number of years
        nBars = len( data.getDifYears() )
        nTeachers = []

        # gets the quantity of teachers per year
        for year in data.getDifYears():
            nTeachers.append(data.getNTheachersPerCategoriePerEstablishmentPerYear( categorie, establishment, year ))
            pass
        teachersPerYear = tuple(nTeachers)
        
        ind = np.arange(nBars)  # the x locations for the bars
        width = 0.5       # the width of the bars
        
        
        plt.subplot(111)
        rects1 = plt.bar(ind + width * 3 / 2, teachersPerYear, width, color='b')
        
        
        plt.ylabel('Number of Teachers\n' + categorie.decode('utf-8'))
        plt.xlabel('Year')
        plt.title('Number of Teachers In\n' + establishment.decode('utf-8'))

        # x bar scale
        plt.xticks( ind + width * 2, (tuple(data.getDifYears())) )

        # y max scale value
        ymax = max(nTeachers)
        ymax = ymax * 13 / 10
        if ymax <= 10:
            ymax = 12
            pass

        # y scale
        yscale = max(nTeachers) / 5
        if yscale == 0:
            yscale = 1
            pass
        
        plt.yticks(xrange(0, ymax, yscale)) 
        
        def autolabel(rects):
            # attach some text labels on top of each bar
            for rect in rects:
                height = rect.get_height()
                plt.text(rect.get_x() + rect.get_width()/2., 1.001*height, '%d'%int(height),
                        ha='center', va='bottom')
        
        autolabel(rects1)

        plt.show()
        pass


    @staticmethod
    def createBarChartOfNTeachersPerDegreePerEstablishmentPerYear( data, degree, establishment ):
        '''
        This method draws a bar char with the number of teachers in a chosen
        establishment and degree along eleven years(2000-2010)
        receives a Database data
        receives an establishment
        '''

        #number of bars = number of years
        nBars = len( data.getDifYears() )
        nTeachers = []

        # gets the quantity of teachers per year
        for year in data.getDifYears():
            nTeachers.append(data.getNTheachersPerDegreePerEstablishmentPerYear( degree, establishment, year ))
            pass
        teachersPerYear = tuple(nTeachers)
        
        ind = np.arange(nBars)  # the x locations for the bars
        width = 0.5       # the width of the bars
        
        
        plt.subplot(111)
        rects1 = plt.bar(ind + width * 3 / 2, teachersPerYear, width, color='b')
        
        
        plt.ylabel('Number of Teachers\n' + degree.decode('utf-8'))
        plt.xlabel('Year')
        plt.title('Number of Teachers In\n' + establishment.decode('utf-8'))

        # x bar scale
        plt.xticks( ind + width * 2, (tuple(data.getDifYears())) )

        # y max scale value
        ymax = max(nTeachers)
        ymax = ymax * 13 / 10
        if ymax <= 10:
            ymax = 12
            pass

        # y scale
        yscale = max(nTeachers) / 5
        if yscale == 0:
            yscale = 1
            pass
        
        plt.yticks(xrange(0, ymax, yscale))
        
        def autolabel(rects):
            # attach some text labels on top of each bar
            for rect in rects:
                height = rect.get_height()
                plt.text(rect.get_x() + rect.get_width()/2., 1.001*height, '%d'%int(height),
                        ha='center', va='bottom')
        
        autolabel(rects1)

        plt.show()
        pass
    
